#include"list.hpp"
#include <cstring>
#include<iostream>
#include <system_error>


//------------------------------------------
//function about listdata     --------------
//------------------------------------------

void ListData::init()
{
    std::cin>>a;
    std::cin>>b;
}

bool ListData::operator==(ListData& comData)
{
   if(this->a==comData.a&&this->b==comData.b)
   { 
        return 1;
   }
   else {
   return 0;
   }
}

void ListData::operator=(ListData* data)
{
   this->a=data->a;
   this->b=data->b;
}


void ListData::show()
{
    std::cout<<a<<" "<<b<<"\n";
}

//------------------------------------------
//function about sequencelist --------------
//------------------------------------------

SequenceList* InitList(int size)
{
    SequenceList* list=new SequenceList;
    list->data=new ListData[size];
    list->memSize=size;
    list->dataSize=size;
    for(int i=0;i<size;i++)
    {
        std::cout<<"init the "<<i<<" data:\n";
        list->data[i].init();
    };
    return list;
}




void SequenceList::ClearList()
{
    this->dataSize=0;
} 



int SequenceList::IfEmpty()
{
    if(this->dataSize==0)
    {
        return 0;
    }
    else {
    return 1;
    }

}



int SequenceList::ListLength()
{
    return this->dataSize;
}



ListData& SequenceList::Getelement(int location)
{
    
    return this->data[location-1];
}



int SequenceList::LocateElement(ListData& data)
{
   for(int i=0;i<dataSize;i++)
   {
    if(this->data[i]==data)
    {
        return i;
    }
   }
   return -1; //equal data don`t exist
}



int SequenceList::Insert(int location,ListData& dataGive)//everytime you call it,the size of data will be larger. To be hones,it`s a wast of time
{
   if (this->dataSize==this->memSize) 
{
   memSize++;
   dataSize++;
   ListData* tmp=new ListData[memSize]; //make a larger array in heap 
   for(int i=0;i<memSize-1;i++)         //put data in it
   {
   tmp[i]=this->data[i];
   } 
   delete this->data;                   //destroy previous array
   this->data=tmp;                      //replace by new on
   memcpy(data+location,data-1+location,(dataSize-location+1)*sizeof(ListData));
   this->data[location-1]=dataGive;     //insert new data in location
   return 1;                            //ok~
}
   else 
{
   memcpy(data+location,data-1+location,(dataSize-location+1)*sizeof(ListData));
   this->data[location-1]=dataGive;     //insert new data in location
   return 1;                            //ok~ 
}                           
}

int SequenceList::Delet(int location)
{
    memcpy(data-1+location,data+location,(dataSize-location+1)*sizeof(ListData));
    dataSize--;
    return 0;
}


void SequenceList::ShowData()
{
    for(int i=0;i<dataSize;i++)
    {   
        std::cout<<"the "<<i<<"data is:";
        this->data[i].show();
    }
}


ListData& SequenceList::operator [](int i)
{
    return data[i];
}




 SequenceList::~SequenceList()
{
  delete[]  this->data;
  std::cout<<"over\n";
}

